package com.ys.ylideas.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ys.ylideas.YlIdeasApplication;
import com.ys.ylideas.controller.YiIdeaController;
import com.ys.ylideas.entity.IdeaEntity;

public class CreativeOperation {
	private static final Logger logger = LoggerFactory.getLogger(YlIdeasApplication.class);
	/**
	 * type为0表示增加，type为1表示修改
	 * @param jsonObj
	 * @param type
	 * @return
	 */
	public static Map<String, String> addOrUpdate(Map<String, Object> jsonObj,int type){
		Map<String, String> map = new HashMap<>();
		if(jsonObj.get("status").toString().equals("failure")){
			Properties prop = new Properties();     
	        try{
	            //读取属性文件a.properties
	        	prop.load(YiIdeaController.class.getClassLoader().getResourceAsStream("error_code.properties"));
	            Iterator<String> it=prop.stringPropertyNames().iterator();
	            while(it.hasNext()){
	            	String key = it.next();
	            	logger.debug(key+"===="+jsonObj.get("code").toString());
	                if (key.equals(jsonObj.get("code").toString())) {
	                	logger.debug(jsonObj.get("code").toString());
	                	map.put("msg",prop.getProperty(key));
						return map;
					}
	            }
	        }
	        catch(Exception e){
	        	map.put("msg",e.toString());
				return map;
	        }
		}else{
			if (type == 0) {
				map.put("creativeId",jsonObj.get("creativeId").toString());
				return map;
			}else if(type == 1){
				map.put("msg","success");
				return map;
			}
		}
		map.put("msg","failure");
		return map;
	}

}
